package com.macro.mall.portal.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.api.ResultCode;
import com.macro.mall.common.exception.Asserts;
import com.macro.mall.common.utils.Assist;
import com.macro.mall.common.vo.CurCourseBaseResp;
import com.macro.mall.mapper.CurCourseBaseMapper;
import com.macro.mall.mapper.MbrMemberCollectMapper;
import com.macro.mall.mapper.MbrMemberPraiseMapper;
import com.macro.mall.mapper.MbrMemberVideoMapper;
import com.macro.mall.model.CurCourseBase;
import com.macro.mall.portal.domain.MemberDetails;
import com.macro.mall.portal.service.CurCourseBaseService;
import com.macro.mall.portal.service.MbrMemberCollectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * CurCourseBase的服务接口的实现类
 *
 * @author gaodx
 */
@Slf4j
@Service
public class CurCourseBaseServiceImpl implements CurCourseBaseService {

    @Value("${log.isDebugEnabled:false}")
    private boolean isDebugEnabled;

    @Autowired
    private CurCourseBaseMapper curCourseBaseMapper;

    @Autowired
    private MbrMemberCollectMapper mbrMemberCollectMapper;

    @Autowired
    private MbrMemberCollectService mbrMemberCollectService;

    @Autowired
    private MbrMemberVideoMapper mbrMemberVideoMapper;
    @Autowired
    private MbrMemberPraiseMapper mbrMemberPraiseMapper;


    @Override
    public List<CurCourseBase> find(CurCourseBase value) {
        //TODO这里可以做通过Assist做添加查询
        List<CurCourseBase> result = curCourseBaseMapper.selectCurCourseBase(null);
        if (isDebugEnabled) {
            log.debug("执行获取CurCourseBase数据集-->结果:", result);
        }
        return result;
    }

    @Override
    public List<CurCourseBaseResp> findBycondition(String searchName, Integer courseType, String orderBy, Integer pageNum, Integer pageSize) {
        //TODO这里可以做通过Assist做添加查询
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(orderBy);
        List<CurCourseBaseResp> result = curCourseBaseMapper.findByconditionOrderBy(searchName, courseType);
        if (isDebugEnabled) {
            log.debug("执行获取CurCourseBase数据集-->结果:", result);
        }
        return result;
    }


    @Override
    public CurCourseBaseResp findOne(Long id) {

        if (id == null) {
            if (isDebugEnabled) {
                log.debug("执行通过CurCourseBase的id获得CurCourseBase对象-->失败:id不能为空");
            }
            Asserts.fail("执行通过CurCourseBase的id获得CurCourseBase对象-->失败:id不能为空");
        }
        Long userId = null;
        try {
            SecurityContext ctx = SecurityContextHolder.getContext();
            Authentication auth = ctx.getAuthentication();
            MemberDetails memberDetails = (MemberDetails) auth.getPrincipal();
            userId = memberDetails.getUmsMember().getId();
        } catch (Exception e) {
            log.error("",e);
            userId = 0L;
        }
        CurCourseBaseResp result = curCourseBaseMapper.selectPortalCurCourseBaseById(id);
        if(StrUtil.isBlankIfStr(result)) {
            return result;
        }
        Assist assist = new Assist();
        assist.setRequires(Assist.whereRequire("course_id = ", id), Assist.whereRequire("and member_id = ", userId));
        long mbrMemberCollectRowCount = mbrMemberCollectMapper.getMbrMemberCollectRowCount(assist);
        if (mbrMemberCollectRowCount == 0) {
            result.setCollectCount("0");
        } else {
            result.setCollectCount("1");
        }
        Assist assist1 = new Assist();
        assist1.setRequires(Assist.whereRequire("target_type = ", 1),
                Assist.whereRequire("and target_pid = ", id),
                Assist.whereRequire("and member_id = ", userId));
        long mbrMemberVideoRowCount = mbrMemberVideoMapper.getMbrMemberVideoRowCount(assist1);
        if (mbrMemberVideoRowCount == 0) {
            result.setLearning("0");
        } else {
            result.setLearning("1");
        }

        Assist assist2 = new Assist();
        assist2.setRequires(Assist.whereRequire("target_type = ", 1),
                Assist.whereRequire("and target_id = ", id),
                Assist.whereRequire("and member_id = ", userId));
        long mbrMemberPraiseRowCount = mbrMemberPraiseMapper.getMbrMemberPraiseRowCount(assist2);
        if (mbrMemberPraiseRowCount == 0) {
            result.setPraise("0");
        } else {
            result.setPraise("1");
        }

        if (isDebugEnabled) {
            log.debug("执行通过CurCourseBase的id获得CurCourseBase对象-->结果:", result);
        }
        return result;
    }

    @Override
    public CommonResult saveNotNull(CurCourseBase value) {
        if (value == null) {
            if (isDebugEnabled) {
                log.debug("执行将CurCourseBase中属性值不为null的数据保存到数据库-->失败:对象不能为空");
            }
            Asserts.fail("失败:对象不能为空");
        }
        int result = 0;
        try {
            result = curCourseBaseMapper.insertNotNullCurCourseBase(value);
        } catch (Exception e) {
            log.error("",e);
        }
        if (isDebugEnabled) {
            log.debug("执行将CurCourseBase中属性值不为null的数据保存到数据库-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public CommonResult updateNotNullById(CurCourseBase value) {
        if (value == null) {
            if (isDebugEnabled) {
                log.debug("执行通过CurCourseBase的id更新CurCourseBase中属性不为null的数据-->失败:对象为null");
            }
            Asserts.fail("执行通过CurCourseBase的id更新CurCourseBase中属性不为null的数据-->失败:对象为null");
        }
        int result = 0;
        try {
            result = curCourseBaseMapper.updateNotNullCurCourseBaseById(value);
        } catch (Exception e) {
            log.error("",e);
        }
        if (isDebugEnabled) {
            log.debug("执行通过CurCourseBase的id更新CurCourseBase中属性不为null的数据-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public CommonResult deleteById(Long id) {
        if (id == null) {
            if (isDebugEnabled) {
                log.debug("执行通过CurCourseBase的id删除CurCourseBase-->失败:id不能为空");
            }
            Asserts.fail("执行通过CurCourseBase的id删除CurCourseBase-->失败:id不能为空");
        }
        int result = 0;
        try {
            result = curCourseBaseMapper.deleteCurCourseBaseById(id);
        } catch (Exception e) {
            log.error("",e);
        }
        if (isDebugEnabled) {
            log.debug("执行通过CurCourseBase的id删除CurCourseBase-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public List<CurCourseBase> findMbrMemberCollect(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Long userId = null;
        try {
            SecurityContext ctx = SecurityContextHolder.getContext();
            Authentication auth = ctx.getAuthentication();
            MemberDetails memberDetails = (MemberDetails) auth.getPrincipal();
            userId = memberDetails.getUmsMember().getId();
        } catch (Exception e) {
           log.error("",e);
            Asserts.fail(ResultCode.UNAUTHORIZED);
        }
        return curCourseBaseMapper.findMbrMemberCollect(userId);

    }


}